package com.example.wto.myapplication;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.ArrayAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by wto on 4/15/2017.
 */

public class UtilTeacherSql
{
    private UtilSql utilSql=null;
    public UtilTeacherSql(UtilSqlHelper sqlHelper)
    {
        utilSql=new UtilSql(sqlHelper);
    }
    public void insert(Teacher teacher)
    {
        String t_id=teacher.getId();
        String name=teacher.getName();
        String s_id=teacher.getS_id();
        String sql="insert into teacher_table (s_id,t_id,name) values(?,?,?)";
        List<Object> params=new ArrayList<Object>();
        params.add(s_id);
        params.add(t_id);
        params.add(name);

        utilSql.update(sql,params);
        List<DayOfWeek> weekList=teacher.getWeekList();

        for(int i=0;i<weekList.size();i++)
        {
            params.clear();
            params.add(s_id);
            params.add(t_id);
            List<Course> courseList=weekList.get(i).getCourseList();
            String sql_one="insert into week_teacher_table values(?,?,?,?,?,?,?,?,?,?,?)";
            params.add(weekList.get(i).getWeekName());
            params.add(i+"");
            for(int j=0;j<courseList.size();j++)
            {
                params.add(courseList.get(j).toString());
            }
            utilSql.update(sql_one,params);

        }
    }
    public Teacher select(String s_id,String t_id)
    {
        boolean flag=false;
        String sql="select t.s_id,t.t_id,name,order_t,week_name,week_one,week_two,week_three,week_four,week_five,week_six,week_seven from teacher_table t ,week_teacher_table w where t.s_id = w.s_id and t.t_id = w.t_id and t.s_id=? and t.t_id=? order by order_t;";
        Teacher teacher=new Teacher();
        Cursor cursor=utilSql.select(sql,new String[]{s_id,t_id});
        List<DayOfWeek> weekList=new ArrayList<DayOfWeek>();
        while(cursor.moveToNext())
        {
            List<Course> courses=new ArrayList<Course>();
            DayOfWeek weekday=new DayOfWeek();
            String teacher_id=cursor.getString(cursor.getColumnIndex("t_id"));
            String teacher_s_id=cursor.getString(cursor.getColumnIndex("s_id"));
            String name=cursor.getString(cursor.getColumnIndex("name"));
            //添加课程
            courses.add(new Course(cursor.getString(cursor.getColumnIndex("week_one"))));
            courses.add(new Course(cursor.getString(cursor.getColumnIndex("week_two"))));
            courses.add(new Course(cursor.getString(cursor.getColumnIndex("week_three"))));
            courses.add(new Course(cursor.getString(cursor.getColumnIndex("week_four"))));
            courses.add(new Course(cursor.getString(cursor.getColumnIndex("week_five"))));
            courses.add(new Course(cursor.getString(cursor.getColumnIndex("week_six"))));
            courses.add(new Course(cursor.getString(cursor.getColumnIndex("week_seven"))));
            //设置星期的名字
            weekday.setWeekName(cursor.getString(cursor.getColumnIndex("week_name")));
            //填充数据weekday数据
            weekday.getCourseList().clear();
            weekday.getCourseList().addAll(courses);
            //填充teacher数据
            weekList.add(weekday);
            teacher.setId(teacher_id);
            teacher.setName(name);
            teacher.setS_id(teacher_s_id);
            flag=true;
        }
        cursor.close();
        utilSql.close();
        if(flag)
        {
            teacher.getWeekList().clear();
            teacher.getWeekList().addAll(weekList);
            return teacher;
        }
        else
            return null;
    }
}
